#include <SPI.h>
#include <ble.h>
#include <Servo.h> 
 
#define DIGITAL_OUT_PIN    4
#define DIGITAL_IN_PIN     5
#define PWM_PIN            6
#define SERVO_PIN          7
#define ANALOG_IN_PIN      A5

Servo myservo;

void setup()
{
  Serial.begin(9600);
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(LSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  SPI.begin();

  

  ble_begin();
  
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  
}

void loop()
{
  static boolean analog_enabled = false;
  static byte old_state = LOW;
  
  // If data is ready
  while(ble_available())
  {
    // read out command and data
    byte data[4];
    data[1] = ble_read();
    data[2] = ble_read();
    data[3] = ble_read();
    data[4] = ble_read();
  
  
  int drink = 0;
  
  for(int i = 1; i < 4; i++){
    if (data[i] == 0x01)
      drink = i;
  }
   if(drink != 0){
   digitalWrite(1+drink, HIGH);
   delay(2000);
   digitalWrite(1+drink, LOW);
   }
  }
  if (!ble_connected())
  {
    analog_enabled = false;
    digitalWrite(DIGITAL_OUT_PIN, LOW);
  }
  
  // Allow BLE Shield to send/receive data
  ble_do_events();  
}



